<template>
  <div class="capture-history">
    <div class="section-title">
      <el-icon><Camera /></el-icon>
      抓拍历史记录
    </div>
    
    <div class="filter-section">
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :shortcuts="shortcuts"
        @change="handleDateChange"
      />
    </div>

    <div class="photos-grid">
      <el-empty v-if="photos.length === 0" description="暂无抓拍记录" />
      <el-card v-else v-for="photo in photos" :key="photo.id" class="photo-card" shadow="hover">
        <div class="photo-info">
          <span class="time">{{ formatDate(photo.createTime) }}</span>
        </div>
        <el-image
          :src="photo.photoUrl"
          :preview-src-list="[photo.photoUrl]"
          fit="cover"
          class="photo-image"
        >
          <template #error>
            <div class="image-error">
              <el-icon><Picture /></el-icon>
              图片加载失败
            </div>
          </template>
        </el-image>
      </el-card>
    </div>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[12, 24, 36, 48]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Camera, Picture } from '@element-plus/icons-vue'
import { getRecentPhotos } from '@/api'
import dayjs from 'dayjs'

const photos = ref([])
const dateRange = ref([])
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(0)

const shortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '最近一个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]

const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

const fetchPhotos = async () => {
  try {
    // 假设设备ID为1，这里需要根据实际情况修改
    const deviceId = 1
    const response = await getRecentPhotos(
      deviceId, 
      pageSize.value,
      dateRange.value?.[0],
      dateRange.value?.[1]
    )
    photos.value = response || []
  } catch (error) {
    console.error('获取抓拍历史失败:', error)
    ElMessage.error('获取抓拍历史失败')
  }
}

const handleDateChange = () => {
  currentPage.value = 1
  fetchPhotos()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchPhotos()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchPhotos()
}

onMounted(() => {
  fetchPhotos()
})
</script>

<style scoped>
.capture-history {
  padding: 16px;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 500;
}

.filter-section {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 16px;
}

.photos-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
  flex: 1;
  overflow-y: auto;
}

.photo-card {
  height: 250px;
  display: flex;
  flex-direction: column;
}

.photo-info {
  padding: 8px;
  font-size: 14px;
  color: #666;
}

.photo-image {
  flex: 1;
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.image-error {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #909399;
  font-size: 14px;
}

.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: center;
}
</style> 